package com.mjw.tender.controller;

import com.mjw.tender.api.ArticleService;
import com.mjw.tender.api.FavouriteService;
import com.mjw.tender.domain.Favourite;
import com.mjw.tender.dto.ResponseResult;
import com.mjw.tender.feign.Oauth2Feign;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 点赞列表与收藏列表api
 */
@RestController
@Slf4j
@RequestMapping("/favourites")
public class FavouriteController {

    @Reference(version = "1.0.0")
    FavouriteService favouriteService;

    @Resource
    Oauth2Feign oauth2Feign;

    @Reference(version = "1.0.0")
    ArticleService articleService;

    /**
     *
     * @param favourite
     * @return
     */
    @PostMapping("/")
    public ResponseResult add(@RequestBody Favourite favourite){
        Long id = -1L;
        try{
            id = favouriteService.add(favourite);
        }catch (Exception e){
            log.error(e.getMessage());
            return new ResponseResult(ResponseResult.CodeStatus.FAIL,"哎呀~出了点情况~请稍后再试！",-1);
        }
        return new ResponseResult(ResponseResult.CodeStatus.OK,id);
    }

    /**
     * 按文章ID和用户ID查询点赞、收藏列表
     * @param articleId 文章ID
     * @param userId    用户ID
     * @return
     */
    @GetMapping(value = "/",params = {"articleId","userId"})
    public ResponseResult<List<Favourite>> getByArticleIdAndUserId(@RequestParam("articleId")Long articleId,
                                                 @RequestParam("userId")Long userId){
        List<Favourite> favouriteList = favouriteService.getByArticleIdAndUserId(articleId,userId);
        return new ResponseResult(ResponseResult.CodeStatus.OK,favouriteList);
    }

    /**
     * 根据文章ID 登录用户名 操作类型删除点赞、收藏列表
     * @param articleId
     * @param userName
     * @param actionType
     * @return
     */
    @DeleteMapping(value = "/",params = {"articleId","userName","actionType"})
    public ResponseResult remove(@RequestParam("articleId")Long articleId,
                                 @RequestParam("userName")String userName,
                                 @RequestParam("actionType")Short actionType){
        if(null != beforeDelete(userName)){
            return new ResponseResult(ResponseResult.CodeStatus.FAIL,beforeDelete(userName));
        }
        try {
            boolean isSuccess = favouriteService.remove(articleId,userName,actionType);
        } catch (Exception e) {
            return new ResponseResult(ResponseResult.CodeStatus.FAIL,"取消收藏失败！");
        }
        return new ResponseResult(ResponseResult.CodeStatus.OK,"操作成功!");
    }

    @DeleteMapping(value = "/",params = {"commentId","userName"})
    public ResponseResult removeCommentLike(@RequestParam("commentId")Long commentId,
                                            @RequestParam("userName")String userName){
        if(null != beforeDelete(userName)){
            return new ResponseResult(ResponseResult.CodeStatus.FAIL,beforeDelete(userName));
        }
        try {
            int delete = favouriteService.removeCommentLike(commentId,userName);
        } catch (Exception e) {
            return new ResponseResult(ResponseResult.CodeStatus.FAIL,"取消评论点赞失败！");
        }
        return new ResponseResult(ResponseResult.CodeStatus.OK,"操作成功!");
    }


    /**
     * 删除操作先进行判断要删除的资源和登录人是否一致
     * @param userName
     * @return
     */
    private String beforeDelete(String userName){
        String message = null;
        ResponseResult result = oauth2Feign.getUserInfo();
        Integer code = result.getCode();
        if(code == ResponseResult.CodeStatus.ILLEGAL_TOKEN){ //没token不允许删除
            message = "登录后即可取消关注！~";
        }

        if (code == ResponseResult.CodeStatus.OK) {
            Map data = (Map) result.getData();
            Map user = (Map) data.get("user");
            String confirmName = (String) user.get("username");
            if(!userName.equals(confirmName)){
                message = "不要动别人的收藏列表哦！";
            }
        }
        return message;
    }

    @GetMapping("/total")
    public ResponseResult<Integer> getTotal(){
        Integer total = favouriteService.getTotal();
        return new ResponseResult(ResponseResult.CodeStatus.OK,total);
    }

}
